package Demo2;

public class MyStack {
    public static class ListNode{
        public ListNode next; //存储下一个结点的地址
        public int val; //存储节点的数据
        public ListNode(int data){
            this.val=data;
        }
    }
    public ListNode head; //存储头结点的地址

    public void push(int data){
        //先创建出一个节点
        ListNode node =new ListNode(data);
        //为空时
        if(head==null){
            head=node;
        }else {
            node.next=head;
            head=node;
        }
    }
    public int pop(){
        int ret=-1;
        if (head==null){
            return -1;
        }
        else {
            ret=head.val;
            head=head.next;
        }
        return ret;
    }
    public int peek(){
        if(head==null){
            return -1;
        }
        else {
            return head.val;
        }
    }
    public void display(){
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}
